package com.jrauto.CarAppBackend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrauto.CarAppBackend.entity.dto.MessageDTO;
import com.jrauto.CarAppBackend.service.MessageService;
import com.jrauto.CarAppBackend.utils.R;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : huiMing
 * Date : 2025年09月11日 16:34
 * @version V1.0
 */
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private MessageService messageService;

    /**
     * 发送消息
     */
    @PostMapping("/send")
    public R<MessageDTO> sendMessage(@RequestParam @NotNull Integer roomId,
                                          @RequestParam @NotNull Integer senderId,
                                          @RequestParam @NotNull Integer receiverId,
                                          @RequestParam @NotBlank String messageType,
                                          @RequestParam(required = false) String content) {
        try {
            MessageDTO messageDTO = messageService.sendMessage(roomId, senderId, receiverId,
                    messageType, content);
            return R.ok("发送成功", messageDTO);
        } catch (Exception e) {
//            log.error("发送消息失败：{}", e.getMessage(), e);
            return R.fail("发送消息失败");
        }
    }

    /**
     * 分页查询聊天室消息
     */
    @GetMapping("/room/{roomId}")
    public R<IPage<MessageDTO>> getRoomMessages(@PathVariable Integer roomId,
                                                     @RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "20") Integer size) {
        try {
            IPage<MessageDTO> messagePage = messageService.getRoomMessages(roomId, page, size);
            return R.ok(messagePage);
        } catch (Exception e) {
//            log.error("查询聊天室消息失败：{}", e.getMessage(), e);
            return R.fail("查询消息失败");
        }
    }

    /**
     * 获取用户未读消息
     */
    @GetMapping("/unread/{userId}")
    public R<List<MessageDTO>> getUnreadMessages(@PathVariable Integer userId) {
        try {
            List<MessageDTO> unreadMessages = messageService.getUnreadMessages(userId);
            return R.ok(unreadMessages);
        } catch (Exception e) {
//            log.error("获取未读消息失败：{}", e.getMessage(), e);
            return R.fail("获取未读消息失败");
        }
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/read")
    public R<Void> markMessagesAsRead(@RequestParam @NotNull Integer roomId,
                                           @RequestParam @NotNull Integer userId) {
        try {
            boolean success = messageService.markMessagesAsRead(roomId, userId);
            if (success) {
                return R.ok("操作成功");
            } else {
                return R.fail("操作失败");
            }
        } catch (Exception e) {
//            log.error("标记消息已读失败：{}", e.getMessage(), e);
            return R.fail("操作失败");
        }
    }

    /**
     * 统计未读消息数量
     */
    @GetMapping("/unread/count")
    public R<Integer> countUnreadMessages(@RequestParam @NotNull Integer roomId,
                                          @RequestParam @NotNull Integer userId) {
        try {
            Integer count = messageService.countUnreadMessages(roomId, userId);
            return R.ok(count);
        } catch (Exception e) {
//            log.error("统计未读消息数量失败：{}", e.getMessage(), e);
            return R.fail("统计失败");
        }
    }

    /**
     * 获取最新消息
     */
    @GetMapping("/latest/{roomId}")
    public R<MessageDTO> getLatestMessage(@PathVariable Integer roomId) {
        try {
            MessageDTO latestMessage = messageService.getLatestMessage(roomId);
            return R.ok(latestMessage);
        } catch (Exception e) {
//            log.error("获取最新消息失败：{}", e.getMessage(), e);
            return R.fail("获取最新消息失败");
        }
    }
}
